//用户vuex模块
import {checkLoginReq} from '@/api/user'
const state = {
  role: localStorage.getItem('role'), //用户角色
  token: localStorage.getItem('t_k'), //用户token
}

const mutations = {
  SET_TOKEN(state, data) {
    //数据一分存vuex
    state.token = data
    //数据一分存本地
    localStorage.setItem('t_k', data)
  },
  SET_ROLE(state, data) {
    state.role = data
    localStorage.setItem('role', data)
  },
}

//actions异步方法
const actions = {
  A_login({commit}, data) {
    return new Promise(async resolve => {
      let res = await checkLoginReq(data)
      //解构数据
      let {code, role, token} = res.data
      //判断
      if (code === 0) {
        commit('SET_TOKEN', 'Bearer ' + token)
        commit('SET_ROLE', role)
        resolve()
      }
    })
  },
}

//导出模块
export default {
  namespaced: true,
  state,
  mutations,
  actions,
}
